package com.bra.ui.adapter;

import java.text.SimpleDateFormat;
import java.util.List;




import com.bra.R;
import com.bra.bean.ReceiverMsg;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * 我的消息adapter
 * @author lenovo
 *
 */
public class MsgAdapter extends BaseAdapter {
	private Context context;
	private List<ReceiverMsg> receive_list;
	public MsgAdapter(Context context) {
		this.context = context;
	}

	@Override
	public int getCount() {
		return receive_list==null ?0:receive_list.size();
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View convertView, ViewGroup arg2) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.adapter_mymsg_item, null);
			holder.iv_noread = (ImageView) convertView
					.findViewById(R.id.iv_noread);
			holder.tv_msg_tongzhi = (TextView) convertView
					.findViewById(R.id.tv_msg_tongzhi);
			holder.tv_msg_time = (TextView) convertView
					.findViewById(R.id.tv_msg_time);
			holder.tv_content = (TextView) convertView
					.findViewById(R.id.tv_content);
			holder.tv_content1 =  (TextView) convertView
					.findViewById(R.id.tv_content1);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		ReceiverMsg msg = receive_list.get(arg0);
		if (msg.is_select) {
			convertView.setBackgroundColor(context.getResources().getColor(R.color.list_item_select));
			holder.tv_content.setVisibility(View.GONE);
			holder.tv_content1.setVisibility(View.VISIBLE);
		}else{
			convertView.setBackgroundColor(context.getResources().getColor(R.color.white));
			holder.tv_content.setVisibility(View.VISIBLE);
			holder.tv_content1.setVisibility(View.GONE);
		}
		if ("1".equals(msg.is_read)) {
			holder.iv_noread.setVisibility(View.INVISIBLE);
		}else{
			holder.iv_noread.setVisibility(View.VISIBLE);
		}
		holder.tv_msg_tongzhi.setText(msg.push_title);
		//判断 
		String push_time =msg.push_time;
		if (push_time.matches("[0-9]+")) {
			SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm");
			String time = sdf.format(Long.parseLong(push_time)*1000);
			holder.tv_msg_time.setText(time);
		}else{
			holder.tv_msg_time.setText(msg.push_time);
		}
		holder.tv_content.setText(msg.push_content);
		holder.tv_content1.setText(msg.push_content);
		return convertView;
	}

	public class ViewHolder {
		public ImageView iv_noread;
		public TextView tv_msg_tongzhi;
		public TextView tv_msg_time;
		public TextView tv_content;
		public TextView tv_content1;
	}

	public void setData(List<ReceiverMsg> receive_list) {
		this.receive_list = receive_list;
		notifyDataSetChanged();
	}
}
